//
//  SelectProgressViewController.m
//  OAProject
//
//  Created by yhj on 2017/4/17.
//  Copyright © 2017年 cdnunion. All rights reserved.
//  选择流程列表主界面

#import "SelectProgressViewController.h"
#import "TaskProgressModel.h"
#import "TaskListViewController.h"


static NSString *TaskProcessTableViewCellIdentifier=@"TaskProcessTableViewCellIdentifier";
@interface SelectProgressViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *taskProgressArr;

@end

@implementation SelectProgressViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self GetData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title=@"请选择流程";

    _taskProgressArr=[NSMutableArray array];

    [self.view addSubview:self.tableView];

}

-(void)GetData
{
    self.tableView.mj_header=[MJRefreshNormalHeader
                              headerWithRefreshingBlock:^{
                                  [self GetTaskProgressListData];
                              }];
    [self.tableView.mj_header beginRefreshing];
}

-(void)GetTaskProgressListData
{
    [self.taskProgressArr removeAllObjects];

    // 获取流程列表
    [API GetTaskProgressListWithTaskID:self.taskID success:^(id res) {

        NSLog(@"sdjksdjssjd---%@",res);

        if ([res[@"rt"] isEqual:@(1)])
        {
            if ([res[@"rows"] count]>0)
            {
                for (NSDictionary *dic in res[@"rows"])
                {
                    TaskProgressModel *model=[TaskProgressModel yy_modelWithDictionary:dic];
                    [self.taskProgressArr addObject:model];
                }
            }

            [self.tableView.mj_header endRefreshing];

            [self.tableView reloadData];

        }
        else
        {
            [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
        }

    } failure:^(NSInteger stateCode) {

    }];
}

// tableView
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH) style:UITableViewStylePlain];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=MAIN_BACK_COLOR;
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:TaskProcessTableViewCellIdentifier];
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.taskProgressArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:TaskProcessTableViewCellIdentifier];
    TaskProgressModel *model=self.taskProgressArr[indexPath.row];
    cell.textLabel.text=model.title;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    TaskProgressModel *model=self.taskProgressArr[indexPath.row];


    UIAlertView *alert=[UIAlertView alertViewWithTitle:@"你确定要拒绝到流程" message:model.title cancelButtonTitle:@"取消" didDismissBlock:^(UIAlertView *alertView,NSInteger buttonIndex) {

        if (buttonIndex==0)
        {
            [API CheckTaskID:self.taskID ac:@"refuse" turn_id:model.step_id memo:@"" success:^(id res) {

                if ([res[@"rt"] isEqual:@(1)])
                {
                   [self.view makeToast:@"任务验收拒绝成功" duration:1 position:CSToastPositionCenter];

                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                        // 返回到任务列表界面
                        for (UIViewController *vc in self.navigationController.viewControllers)
                        {
                            if ([vc isKindOfClass:[TaskListViewController class]])
                            {
                                [self.navigationController popToViewController:vc animated:NO];
                            }
                        }
                    });
                }
                else
                {
                    [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
                }

            } failure:^(NSInteger stateCode) {

            }];
        }
    } otherButtonTitles:@"确定", nil];
    [alert show];

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kmargin*5;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView=[UIView new];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return kmargin;
}
@end
